/**正在研发 */
const router = require('koa-router')();
const DB = require('../db');

/**
 * 新增研发项目
 * @param {string} content 内容
 * @param {array} contriId 贡献人员id
 * @param {number} devProg 研发进度
 */
router.post('/addDevItem', async (ctx, next) => {
  const userName = ctx.request.header.username;
  if (!userName) {
    ctx.body = {
      code: '0008',
      message: '请先登录',
      data: {}
    }
    return;
  }
  const { content, contriId, devProg } = ctx.request.body;
  if (!content || !contriId || (devProg !== 0 && !devProg)) {
    ctx.body = {
      code: '0002',
      message: '缺少必要参数',
      data: {}
    }
    return;
  }
  if (!Array.isArray(contriId)) {
    ctx.body = {
      code: '0001',
      message: '贡献人员id参数必须是数组',
      data: {}
    }
    return;
  }
  if (contriId.length === 0) {
    ctx.body = {
      code: '0002',
      message: '贡献人员不能为空',
      data: {}
    }
    return;
  }
  let avatarArr = [];
  for (let i = 0; i < contriId.length; i++) {
    const ra = await DB.find('users', { uid: contriId[i] });
    avatarArr.push(ra[0].avatarUrl);
  }
  const addData = {
    key: String(Date.now()) + '_' + userName,
    startDate: Date.now(),
    content,
    contriId,
    avatarArr,
    devProg
  };
  await DB.add('underDevelopment', addData);
  ctx.body = {
    code: '0000',
    message: '添加成功',
    data: addData
  };
})

/**
 * 删除研发项目
 * @param {string} id 对应key值
 */
router.post('/deleteDevItem', async (ctx, next) => {
  const userName = ctx.request.header.username;
  if (!userName) {
    ctx.body = {
      code: '0008',
      message: '请先登录',
      data: {}
    }
    return;
  }
  const { id } = ctx.request.body;
  if (!id) {
    ctx.body = {
      code: '0002',
      message: '缺少必要参数',
      data: {}
    }
    return;
  }
  const r = await DB.find('underDevelopment', { key: id });
  await DB.delOne('underDevelopment', { key: id });
  ctx.body = {
    code: '0000',
    message: '删除成功',
    data: r[0]
  }
})

/**
 * 修改研发项目
 * @param {string} id 对应key值
 * @param {string} content 内容
 * @param {array} contriId 贡献人员
 * @param {number} devProg 研发进度
 */
router.post('/reviseDevItem', async (ctx, next) => {
  const userName = ctx.request.header.username;
  if (!userName) {
    ctx.body = {
      code: '0008',
      message: '请先登录',
      data: {}
    }
    return;
  }
  const { id, content, contriId, devProg } = ctx.request.body;
  if (!id || !content || !contriId || (devProg !== 0 && !devProg)) {
    ctx.body = {
      code: '0002',
      message: '缺少必要参数',
      data: {}
    }
    return;
  }
  if (!Array.isArray(contriId)) {
    ctx.body = {
      code: '0001',
      message: '贡献人员id参数必须是数组',
      data: {}
    }
    return;
  }
  if (contriId.length === 0) {
    ctx.body = {
      code: '0002',
      message: '贡献人员不能为空',
      data: {}
    }
    return;
  }
  let avatarArr = [];
  for (let i = 0; i < contriId.length; i++) {
    const ra = await DB.find('users', { uid: contriId[i] });
    avatarArr.push(ra[0].avatarUrl);
  }
  await DB.updateValue('underDevelopment', { key: id }, { content, contriId, avatarArr, devProg });
  const r = await DB.find('underDevelopment', { key: id });
  ctx.body = {
    code: '0000',
    message: '修改成功',
    data: r[0]
  }
})

/**
 * 获取研发项目列表
 */
router.get('/devItemList', async (ctx, next) => {
  const r = await DB.find('underDevelopment');
  ctx.body = {
    code: '0000',
    message: '获取成功',
    data: r
  }
})

/**
 * 发布研发项目
 * @param {string} id 对应key值
 */
router.post('/publishDevItem', async (ctx, next) => {
  const userName = ctx.request.header.username;
  if (!userName) {
    ctx.body = {
      code: '0008',
      message: '请先登录',
      data: {}
    }
    return;
  }
  const { id } = ctx.request.body;
  if (!id) {
    ctx.body = {
      code: '0002',
      message: '缺少必要参数，发布失败',
      data: {}
    }
    return;
  }
  const r = await DB.find('underDevelopment', { key: id });
  if (!r || r.length === 0) {
    ctx.body = {
      code: '0013',
      message: '参数id不正确，发布失败',
      data: {}
    }
    return;
  }
  if (r[0].devProg < 100) {
    ctx.body = {
      code: '0021',
      message: '项目未完成，发布失败',
      data: {}
    }
    return;
  }
  await DB.delOne('underDevelopment', { key: id });
  await DB.add('previousFonc', {
    key: String(Date.now()) + '_' + userName,
    publishDate: Date.now(),
    content: r[0].content,
    contriId: r[0].contriId,
    avatarArr: r[0].avatarArr,
    likeNum: 0,
    likeList: []
  });
  ctx.body = {
    code: '0000',
    message: '发布成功',
    data: r[0]
  }
})

module.exports = router;